﻿using EmitMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSite.Core;
using WebSite.Data.NH.Repositories;
using WebSite.Model;
using WebSite.Web.Models;
using WebSite.Web.SessionAndFilter;

namespace WebSite.Web.Controllers.Admin
{
    [AuthorAttribute]
    public class LinksController : Controller
    {
         private readonly ILinksRepository lr;

         public LinksController(ILinksRepository lr)
        {
            this.lr = lr;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Links(string title, int page = 1, int pageSize = 15)
        {
            var user = Sessions.User;

            GridData<LinksVM> data = new GridData<LinksVM>();
            ObjectsMapper<LinksInfo, LinksVM> mapper =
                   ObjectMapperManager.DefaultInstance.GetMapper<LinksInfo, LinksVM>();

            var where = PredicateExtension.True<LinksInfo>();
            where = where.AndAlso(u => u.CompanyId == user.CompanyId);

            if (!string.IsNullOrEmpty(title))
            {
                where = where.AndAlso(u => u.Title.Contains(title));
            }

            var count = lr.LoadCount(where);
            var list = lr.LoadAll(where, page, pageSize, new OrderByExpression<LinksInfo, int>(u => u.Id));
            data.Rows = mapper.MapEnum(list).ToList();
            data.Total = count;
            return Json(data);
        }

        public ActionResult Edit(int Id = 0)
        {
            LinksVM link = new LinksVM();
            ObjectsMapper<LinksInfo, LinksVM> mapper =
                   ObjectMapperManager.DefaultInstance.GetMapper<LinksInfo, LinksVM>();

            if (Id > 0)
            {
                var res = lr.GetLink(Id);
                link = mapper.Map(res);
            }

            return View(link);
        }

        [HttpPost]
        public ActionResult Edit(LinksVM av)
        {
            if (ModelState.IsValid)
            {
                ObjectsMapper<LinksVM, LinksInfo> mapper =
                  ObjectMapperManager.DefaultInstance.GetMapper<LinksVM, LinksInfo>();

                var user = Sessions.User;

                if (av.Id == 0)
                {
                    var a = mapper.Map(av);
                    a.CompanyId = user.CompanyId;
                    bool res = lr.AddLink(a);
                    return Json(res);
                }
                else
                {
                    var a = mapper.Map(av);
                    bool res = lr.UpdateLink(a);
                    return Json(res);
                }

            }

            return View();
        }

        [HttpPost]
        public ActionResult DeleteLink(int id)
        {
            var b = false;
            b = lr.DeleteLink(id);
            return Json(b);
        }

    }
}
